package com.shell.governor.modules.service.impl;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.file.utils.FileHelper;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.dao.PubNoticeDao;
import com.shell.governor.modules.po.PubNotice;
import com.shell.governor.modules.service.PubNoticeService;

import tk.mybatis.mapper.entity.Example;

@Service
public class PubNoticeServiceImpl extends PersistentServiceImpl<PubNotice> implements PubNoticeService {
	@Autowired
	PubNoticeDao noticeDao;

	@Pagination
	public List<PubNotice> getNoticeByPage(PageLimit limit, PubNotice notice) {
		if (!StringUtil.emptyAndNull(notice.getTitle())) {
			notice.setTitle(StringUtil.getParameterLike(notice.getTitle()));
		}
		return this.noticeDao.getNotices(notice);
	}

	public RetMsg deleteNotice(PubNotice notice) {
		int flag = delete(notice);
		if (flag == 1) {
			return new RetMsg("success", "��������������");
		}
		return new RetMsg("error", "��������������");
	}

	public RetMsg saveOrUpdateNotice(PubNotice notice, String fileIds) throws Exception {
		int flag = 0;
		String noticeId = notice.getNoticeId();
		if (StringUtil.emptyAndNull(notice.getNoticeId())) {
			if (StringUtil.notEmpty(notice.getAppId())) {
				String[] appIds = notice.getAppId().split(",");
				for (String appId : appIds) {
					PubNotice snotice = new PubNotice();
					BeanUtils.copyProperties(snotice, notice);
					snotice.setNoticeId(SystemUtil.getUUID());
					snotice.setAppId(appId);
					snotice.setCreateTime(SysDateUtil.getSystemDateTime());
					snotice.setCreatorId(CommonDataUtil.getUserId());
					noticeId = snotice.getNoticeId();
					flag = save(snotice);
				}
			}
		} else {
			notice.setEditTime(SysDateUtil.getSystemDateTime());
			notice.setEditorId(CommonDataUtil.getUserId());
			flag = updateNotNull(notice);
		}
		if (!StringUtil.emptyAndNull(fileIds)) {
			FileHelper.clearRelateId(noticeId, notice.getType());
			FileHelper.setRelateId(fileIds, noticeId, notice.getType());
		}
		if (flag == 1) {
			return new RetMsg("success", false, "����������");
		}
		return new RetMsg("error", false, "����������");
	}

	public List<PubNotice> getAllNotices(PubNotice notice) {
		Example example = new Example(PubNotice.class);
		example.setOrderByClause(" front DESC,create_time DESC");
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("noticeState", "1");
		if (!StringUtil.emptyAndNull(notice.getAppId())) {
			criteria.andEqualTo("appId", notice.getAppId());
		}
		if (!StringUtil.emptyAndNull(notice.getType())) {
			criteria.andEqualTo("type", notice.getType());
		}
		if (!StringUtil.emptyAndNull(notice.getNoticeArea())) {
			criteria.andEqualTo("noticeArea", notice.getNoticeArea());
		}
		return selectByExample(example);
	}

	public PubNotice getNoticeDetail(PubNotice notice) {
		return null;
	}

	public RetMsg updateNoticeState(String noticeId, String noticeState) {
		PubNotice notice = (PubNotice) selectByKey(noticeId);
		int i = 0;
		if (null != notice) {
			if ("0".equals(noticeState)) {
				notice.setNoticeState("1");
				i = updateNotNull(notice);
			} else if ("1".equals(noticeState)) {
				notice.setNoticeState("0");
				i = updateNotNull(notice);
			}
			if (i > 0) {
				return new RetMsg("success", true, "������������������");
			}
			return new RetMsg("error", true, "������������������");
		}
		return new RetMsg("error", true, "������������������");
	}
}
